# File name:    governance_complexity_replication
# Last Change:  03/03/2022
# Name:         Julian Limberg, Christoph Knill, Yves Steinebach
# Purpose:      Replication for Article "Condemned to Complexity?" (Governance, 2022)

####1. System setups####
#Load Packages
# install packages from CRAN
p_needed <- c("tidyverse", "texreg","xlsx","countrycode","imputeTS","foreign","magrittr","interplot", "car",
              "ggplot2","gridExtra","grid","ggpubr","scales","varhandle","readstata13","estimatr","lmtest",
              "readxl","cowplot","xtable","margins","rdrobust","broom","devtools")
packages <- rownames(installed.packages())
p_to_install <- p_needed[!(p_needed %in% packages)]
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
lapply(p_needed, require, character.only = TRUE)
#Set Seed
set.seed(2789)


setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

load("./data_governance.RData")

merge

####2. Main Analysis Interaction####

###Table A6
#Institutionalism -- Higher Level of Restrictions, less possibilities to individually tailor options - new legislation leads to diversification in a deterministic way
summary(reg_2 <- lm(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x) + year))
summary(reg_6 <- lm(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x) + year))

#Globalisation -- With growing development of society, new policies automatically more complex due to new complex problems in rich societies
summary(reg_3 <- lm(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x) + year))
summary(reg_7 <- lm(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x) + year))

#Partisan -- Growing political integration spurs the spread of new tools/ ideas -- new policies become more diverse/complex
summary(reg_4 <- lm(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen +  factor(country.x) + year))
summary(reg_8 <- lm(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen +  factor(country.x) + year))


screenreg(l = list(reg_2, reg_3, reg_4, reg_6, reg_7, reg_8), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Green-Growth",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right"
        ))

plot_1 <- interplot(m=reg_2, var1 ="size", var2="instcons", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Institutional Constraints") +
  ggtitle("Institutionalism") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank()) +
  xlim(-0.5,5.5)

plot_histo_1 <- ggplot(merge_env_1, aes(x=instcons)) +
  geom_histogram() +
  ylab("N") +
  xlab("Institutional Constraints") +
  theme_bw() +
  theme(text = element_text(size=13),
        plot.caption = element_blank()) +
  xlim(-0.5,5.5)

plot_2 <- interplot(m=reg_3, var1 ="size", var2="dr_pg", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Political Globalisation") +
  ggtitle("Globalisation ") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(57,101)

plot_histo_2 <- ggplot(merge_env_1, aes(x=dr_pg)) +
  geom_histogram() +
  ylab("N") +
  xlab("Political Globalisation") +
  theme_bw() +
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(57,101)

plot_3 <- interplot(m=reg_4, var1 ="size", var2="GOV_Cab_GG", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Growth-Green") +
  ggtitle("Partisanship") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(-27,16)

plot_histo_3 <- ggplot(merge_env_1, aes(x=GOV_Cab_GG)) +
  geom_histogram() +
  ylab("N") +
  xlab("Growth-Green") +
  theme_bw() +
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(-27,16)


arrange_env_1 <- plot_grid(plot_1, plot_histo_1, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))
arrange_env_2 <- plot_grid(plot_2, plot_histo_2, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))
arrange_env_3 <- plot_grid(plot_3, plot_histo_3, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))


plot_4 <- interplot(m=reg_6, var1 ="size", var2="instcons", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Institutional Constraints") +
  ggtitle("Institutionalism") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(-0.5,5.5)


plot_5 <- interplot(m=reg_7, var1 ="size", var2="dr_pg", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Political Globalisation") +
  ggtitle("Globalisation ") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(57,101)


plot_6 <- interplot(m=reg_8, var1 ="size", var2="GOV_Cab_LR", hist = F) +
  ylab("Marginal Effect of Portfolio Size") +
  xlab("Right-Left") +
  ggtitle("Partisanship") +
  theme_bw() +
  geom_hline(yintercept = 0, linetype ="dashed") + 
  theme(text = element_text(size=13),
        plot.caption = element_blank())
arrange_env <- ggarrange(plot_1, plot_2, plot_3, ncol = 3)+
  xlim(-25,20)

plot_histo_4 <- ggplot(merge_soc_1, aes(x=GOV_Cab_LR)) +
  geom_histogram() +
  ylab("N") +
  xlab("Right-Left") +
  theme_bw() +
  theme(text = element_text(size=13),
        plot.caption = element_blank())+
  xlim(-25,20)

arrange_social_1 <- plot_grid(plot_4, plot_histo_1, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))
arrange_social_2 <- plot_grid(plot_5, plot_histo_2, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))
arrange_social_3 <- plot_grid(plot_6, plot_histo_4, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))

###Figure 3
arrange_env <- ggarrange(arrange_env_1, arrange_env_2, arrange_env_3, ncol = 3)

###Figure 4
arrange_social <- ggarrange(arrange_social_1, arrange_social_2, arrange_social_3, ncol = 3)

####3. Robustness####
###2. Country Clustered SEs

#Institutionalism -- Higher Level of Restrictions, less possibilities to individually tailor options - new legislation leads to diversification in a deterministic way
summary(reg_2 <- lm_robust(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + year + factor(country.x)), cluster = country.x, se_type = "stata")
summary(reg_6 <- lm_robust(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + year + factor(country)), cluster = country.x, se_type = "stata")

#Globalisation -- With growing development of society, new policies automatically more complex due to new complex problems in rich societies
summary(reg_3 <- lm_robust(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + year + factor(country.x)), cluster = country.x, se_type = "stata")
summary(reg_7 <- lm_robust(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + year + factor(country.x)), cluster = country.x, se_type = "stata")


#Partisan -- Growing political integration spurs the spread of new tools/ ideas -- new policies become more diverse/complex
summary(reg_4 <- lm_robust(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + year + factor(country.x)), cluster = country.x, se_type = "stata")
summary(reg_8 <- lm_robust(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + year + factor(country.x)), cluster = country.x, se_type = "stata")



screenreg(l = list(reg_2, reg_3, reg_4, reg_6, reg_7, reg_8), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Green-Growth",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right"
        ))

###PCSEs (Stata)

###PCSEs and AR(1) (Stata)

###Year FEs

#Institutionalism -- Higher Level of Restrictions, less possibilities to individually tailor options - new legislation leads to diversification in a deterministic way
summary(reg_2 <- lm(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))
summary(reg_6 <- lm(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))

#Globalisation -- With growing development of society, new policies automatically more complex due to new complex problems in rich societies
summary(reg_3 <- lm(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))
summary(reg_7 <- lm(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))


#Partisan -- Growing political integration spurs the spread of new tools/ ideas -- new policies become more diverse/complex
summary(reg_4 <- lm(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))
summary(reg_8 <- lm(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(year) + factor(country.x)))



screenreg(l = list(reg_2, reg_3, reg_4, reg_6, reg_7, reg_8), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Green-Growth",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right"
        ))

###Cubic Time Trends
merge_env_1 <- merge_env_1 %>%
  dplyr::mutate(trend = year - 1980,
                trend2 = trend*trend,
                trend3 = trend*trend*trend)

merge_soc_1 <- merge_soc_1 %>%
  dplyr::mutate(trend = year - 1980,
                trend2 = trend*trend,
                trend3 = trend*trend*trend)

#Institutionalism -- Higher Level of Restrictions, less possibilities to individually tailor options - new legislation leads to diversification in a deterministic way
summary(reg_2 <- lm(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))
summary(reg_6 <- lm(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))

#Globalisation -- With growing development of society, new policies automatically more complex due to new complex problems in rich societies
summary(reg_3 <- lm(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))
summary(reg_7 <- lm(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))


#Partisan -- Growing political integration spurs the spread of new tools/ ideas -- new policies become more diverse/complex
summary(reg_4 <- lm(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))
summary(reg_8 <- lm(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(country.x) +trend + trend2 + trend3))



screenreg(l = list(reg_2, reg_3, reg_4, reg_6, reg_7, reg_8), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Green-Growth",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right"
        ))

###No Time Trends

#Institutionalism -- Higher Level of Restrictions, less possibilities to individually tailor options - new legislation leads to diversification in a deterministic way
summary(reg_2 <- lm(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x)))
summary(reg_6 <- lm(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x)))

#Globalisation -- With growing development of society, new policies automatically more complex due to new complex problems in rich societies
summary(reg_3 <- lm(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + factor(country.x)))
summary(reg_7 <- lm(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + factor(country.x)))


#Partisan -- Growing political integration spurs the spread of new tools/ ideas -- new policies become more diverse/complex
summary(reg_4 <- lm(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(country.x)))
summary(reg_8 <- lm(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + factor(country.x)))



screenreg(l = list(reg_2, reg_3, reg_4, reg_6, reg_7, reg_8), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Growth-Green",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right"
        ))

###Public Opinion

summary(reg_1 <- lm(data= merge_env_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_GG + deficit + unemp + inflation + kaopen + econ_rel_post_mean + factor(country.x) + year))
summary(reg_4 <- lm(data= merge_soc_1, diversity ~ size*instcons + log(wdi_gdpcapcur) + dr_pg + GOV_Cab_LR + deficit + unemp + inflation + kaopen + econ_rel_post_mean + factor(country.x) + year))

summary(reg_2 <- lm(data= merge_env_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_GG + deficit + unemp + inflation + kaopen + econ_rel_post_mean + factor(country.x) + year))
summary(reg_5 <- lm(data= merge_soc_1, diversity ~ size*dr_pg + instcons + log(wdi_gdpcapcur) + GOV_Cab_LR + deficit + unemp + inflation + kaopen + econ_rel_post_mean + factor(country.x) + year))

summary(reg_3 <- lm(data= merge_env_1, diversity ~ size*GOV_Cab_GG + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + econ_rel_post_mean +  factor(country.x) + year))
summary(reg_6 <- lm(data= merge_soc_1, diversity ~ size*GOV_Cab_LR + log(wdi_gdpcapcur) + dr_pg + instcons + deficit + unemp + inflation + kaopen + econ_rel_post_mean +  factor(country.x) + year))

screenreg(l = list(reg_1, reg_2, reg_3, reg_4, reg_5, reg_6), include.ci = FALSE,
        include.bic = TRUE, include.aic = TRUE, include.rsquared = T, include.variance = FALSE,  
        include.deviance = F, center = TRUE, digits = 4, include.missings = FALSE, no.margin = TRUE, 
        caption = "Regression Models", caption.above = TRUE, 
        custom.coef.map = list("size" = "Portfolio Size", 
                               "instcons" = "Institutional Constraints",
                               "log(wdi_gdpcapcur)" = "GDP per Capita (logged)",
                               "dr_pg" = "Political Globalisation",
                               "GOV_Cab_GG" = "Green-Growth",
                               "GOV_Cab_LR" = "Left-Right",
                               "unemp" = "Unemployment",
                               "inflation" = "Inflation",
                               "deficit" = "Deficit",
                               "kaopen" = "Capital Account Openness",
                               "size:instcons" = "Size*Institutional Constraints",
                               "size:dr_pg" = "Size*Political Globalisation",
                               "size:GOV_Cab_GG" = "Size*Green-Growth",
                               "size:GOV_Cab_LR" = "Size*Left-Right",
                               "econ_rel_post_mean" = "Economic Conservatism"
        ))


####4. Export####

merge_soc_red <- merge_soc_1 %>%
  dplyr::select(iso3, year, diversity, size) %>%
  dplyr::rename(diversity_soc = diversity,
                size_soc = size)

merge_both <- left_join(merge_env_1, merge_soc_red, by = c("iso3", "year")) %>%
  dplyr::rename(diversity_env = diversity,
                size_env = size) %>%
  dplyr::mutate(iso3n = countrycode(.$iso3, "iso3c", "iso3n", warn = T)) 

write.dta(merge_both, "./full_stata.dta")

####5. Descriptives####

### Figure 2
env_1 <- merge_env_1 %>%
  ggplot(., aes(x=size, y=diversity)) +
  geom_point() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Complexity Environmental") +
  xlab("Size Environmental") +
  geom_hline(yintercept = mean(merge_env_1$diversity)) +
  geom_vline(xintercept = mean(merge_env_1$size))+
  ylim(0.4,1)+
  xlim(0,0.4)



soc_1 <- merge_soc_1 %>%
  ggplot(., aes(x=size, y=diversity)) +
  geom_point() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Complexity Social") +
  xlab("Size Social") +
  geom_hline(yintercept = mean(merge_soc_1$diversity, na.rm = T)) +
  geom_vline(xintercept = mean(merge_soc_1$size, na.rm = T))+
  ylim(0.4,1)+
  xlim(0,0.4)


arrange_patterns <- ggarrange(env_1, soc_1, ncol = 2)


###Figure A2 - Dev Over Time

env_2 <- merge_env_1 %>%
  ggplot(., aes(x=year, y=diversity, group=factor(year))) +
  geom_boxplot() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Complexity Environmental") +
  xlab("Year")+
  ylim(0.4,1)

soc_2 <- merge_soc_1 %>%
  ggplot(., aes(x=year, y=diversity, group=factor(year))) +
  geom_boxplot() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Complexity Social") +
  xlab("Year")+
  ylim(0.4,1)

env_3 <- merge_env_1 %>%
  ggplot(., aes(x=year, y=size, group=factor(year))) +
  geom_boxplot() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Size Environmental") +
  xlab("Year") +
  ylim(0,0.4)


soc_3 <- merge_soc_1 %>%
  ggplot(., aes(x=year, y=size, group=factor(year))) +
  geom_boxplot() +
  theme_bw()+
  theme(text = element_text(size=14))   +
  ylab("Size Social") +
  xlab("Year")+
  ylim(0,0.4)


arrange_patterns <- ggarrange(env_3, soc_3, env_2, soc_2, ncol = 2, nrow = 2)


